# -*- coding: utf-8 -*-
from django.test import TestCase
from Task.models import Task, Job
import json
from mock import patch

# Create your tests here.
class TaskTest(TestCase):
    def test_task_flow(self):
        test_module = "esr_parser.shortcuts.get_mobile_ranking"

        task = Task()
        task.name = "Test"
        task.module = test_module
        task.raw_conf = json.dumps({"keyword": "iphone"})
        task.save()

        # test create job
        job = task.create_job()

        ## check save info db
        assert job.id
        ## check job name
        assert job.name == "Test"
        ## check init retry is 0
        assert job.retry == 0
        ## check job init status is watting
        assert job.status == Job.Status.pending

        ## no dependence
        dependence_is_complete = job.check_dependence_complete()
        ## no dependence auto complete
        assert dependence_is_complete

        ## first start
        with patch(test_module) as mock_module:
            mock_module.return_value = 'test'
            job.start()
        
            assert job.status == Job.Status.success
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 1
            assert job_outputs[0].key == 'output'
            assert mock_module.call_count == 1
        
            ## second start
            job.start()
            assert mock_module.call_count == 1

            ## restart
            mock_module.return_value = 'data'
            job.restart()
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 1
            assert job_outputs[0].key == 'output'
            assert mock_module.call_count == 2

            ## restart check value is dict
            mock_module.return_value = {"key1": "data1", "key2": "data2"}
            job.restart()
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 2
            assert mock_module.call_count == 3


            ## restart check value is nested dict
            mock_module.return_value = {"key1": {"sub_key": 123}, "key2": "data2"}
            job.restart()
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 2
            assert mock_module.call_count == 4


            ## restart check value is unicode string
            mock_module.return_value = {"key1": "中文", "key2": "data2"}
            job.restart()
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 2
            assert mock_module.call_count == 5


            ## restart check value is unicode string
            mock_module.return_value = {"key1": "中文", "key2": "data2"}
            job.name = '中文'
            job.save()
            job.restart()
            job_outputs = job.outputs.all()
            assert len(job_outputs) == 2
            assert mock_module.call_count == 6


        job.logger.info('a')
        assert job.log == ''
        job.logger.info('b')
        assert job.log == ''
        job.logger.info('c')
        assert job.log == ''
        job.logger.info('d')
        assert job.log == ''
        job.logger.info('e')
        assert job.log
